package com.boc.langxiang.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boc.langxiang.R;

/**
 * Created by Li Guoxi on 2016/6/26.
 */
public class NumberPicker extends RelativeLayout implements View.OnClickListener {

    private Context mContext;
    private ImageView plus, minus;

    private int currentNum = 1;

    private int maxNum = -1;

    private OnNumberPickerListener onNumberPickerListener;


    private TextView content;

    public NumberPicker(Context context) {
        super(context);
        this.mContext = context;
        init(context);
    }

    public NumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(context);
    }

    public NumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.number_picker_layout, this);
        plus = (ImageView) findViewById(R.id.plus);
        minus = (ImageView) findViewById(R.id.minus);
        content = (TextView) findViewById(R.id.content_tv);
        content.setText(String.valueOf(currentNum));
        plus.setOnClickListener(this);
        minus.setOnClickListener(this);
    }

    public int getMaxNum() {
        return maxNum;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }

    public int getCurrentNum() {
        return currentNum;
    }

    public void setCurrentNum(int currentNum) {
        this.currentNum = currentNum;
        content.setText(String.valueOf(currentNum));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.plus:
                onPlus();
                break;

            case R.id.minus:
                onMinus();
                break;
        }

    }

    private void onMinus() {
        if (currentNum > 1) {
            currentNum -= 1;
//            content.setText(String.valueOf(currentNum));
            if (onNumberPickerListener != null) {
                onNumberPickerListener.onPicker(currentNum, OnNumberPickerListener._PickerMinus);
            }
        }
    }

    private void onPlus() {
        if (maxNum > 0) {
            if (currentNum == maxNum) {
                onNumberPickerListener.onFailed(currentNum, maxNum);
                return;
            }
        }
        currentNum += 1;
//        content.setText(String.valueOf(currentNum));
        if (onNumberPickerListener != null) {
            onNumberPickerListener.onPicker(currentNum, OnNumberPickerListener._PickerMinus);
        }
    }

    public OnNumberPickerListener getOnNumberPickerListener() {
        return onNumberPickerListener;
    }

    public void setOnNumberPickerListener(OnNumberPickerListener onNumberPickerListener) {
        this.onNumberPickerListener = onNumberPickerListener;
    }

    public interface OnNumberPickerListener {
        final int _PickerAdd = 1001;
        final int _PickerMinus = 1002;

        void onPicker(int num, int type);

        void onFailed(int num, int maxNum);
    }
}
